﻿//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Validation Application Block
//===============================================================================

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.Reflection;
using System.Windows.Forms;
using Microsoft.Practices.EnterpriseLibrary.Validation.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Validation;

namespace EntLibContrib.Validation.Configuration.Design.UI
{
	/// <summary>
	/// Test Validator user interface
	/// </summary>
	public partial class TestValidatorUI : Form
	{
		#region Fields
		private CultureInfoConverter ciConverter = new CultureInfoConverter();
		private ValidatorData validatorData;
		private Type targetType;
		#endregion

		#region Construction
		/// <summary>
		/// Initializes a new instance of the <see cref="TestValidatorUI"/> class.
		/// </summary>
		/// <param name="validatorData">The validator data.</param>
		public TestValidatorUI(ValidatorData validatorData)
		{
			this.validatorData = validatorData;
			targetType = typeof(string);
			InitializeComponent();

			Text = String.Format(CultureInfo.CurrentCulture, Text, validatorData.Name);

			foreach (CultureInfo StdValue in ciConverter.GetStandardValues())
			{
				if (!StdValue.IsNeutralCulture)
				{
					comboBox2.Items.Add(ciConverter.ConvertToString(StdValue));
				}
			}
			comboBox2.SelectedItem = ciConverter.ConvertToString(CultureInfo.InvariantCulture);
		}
		#endregion

		#region Event Handlers
		private void button1_Click(object sender, EventArgs e)
		{
			Close();
		}

		private void textBox1_TextChanged(object sender, EventArgs e)
		{
			TestValidator();
		}

		private void comboBox1_TextChanged(object sender, EventArgs e)
		{
			targetType = Type.GetType(comboBox1.Text, false);
			if (targetType != null)
			{
				comboBox1.ForeColor = Color.Black;

				TestValidator();
			}
			else
			{
				comboBox1.ForeColor = Color.Red;
			}
		}

		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
		private void comboBox2_TextChanged(object sender, EventArgs e)
		{
			try
			{
				ciConverter.ConvertFromString(comboBox2.Text);
				comboBox2.ForeColor = Color.Black;
				TestValidator();

			}
			catch
			{
				comboBox2.ForeColor = Color.Red;
			}
		}
		#endregion

		#region Private Methods
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
		private void TestValidator()
		{
			try
			{
				object valueToTest = TypeDescriptor.GetConverter(targetType).ConvertFrom(null, ciConverter.ConvertFrom(comboBox2.Text) as CultureInfo, textBox1.Text);
				Validator validator = (Validator)validatorData.GetType().GetMethod("DoCreateValidator", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance, null, new Type[] { typeof(Type) }, new ParameterModifier[0]).Invoke(validatorData, new object[] { targetType });
				List<ValidationResult> result = new List<ValidationResult>(validator.Validate(valueToTest));
				if (result.Count > 0)
				{
					label2.Text = "Value is invalid: " + ((result[0].Message.Length == 0) ? "<empty>" : result[0].Message);
				}
				else
				{
					label2.Text = "Value is valid";
				}
			}
			catch (Exception ex)
			{
				label2.Text = ex.Message;
			}

			toolTip1.SetToolTip(label2, label2.Text);
		}
		#endregion
	}
}